import {
  HttpApi
} from '../../../../model/index.js'
const httpApi = new HttpApi

Page({
  data: {
    windowHeight: 100,
    suggest: '',
    communityId: "",
    communityName: "",
    tempFilePaths: [],
    src: "",
    title: "",
    content: "",
    uploadUrls: []
  },
  /**
   * 生命周期函数--监听页面加载
   */
  onLoad: function (options) {
    var that = this;
    var login = wx.getStorageSync('login')
   
    //设置scroll-view高度
    wx.getSystemInfo({
      success: function (res) {
        that.setData({ windowHeight: res.windowHeight - 100 });
      }
    });
  },
  inputTitle: function (e) {
    this.setData({
      title: e.detail.value
    })
  },
  inputContent: function (e) {
    this.setData({
      content: e.detail.value
    })
  },
  addImage(e) {
    var _this = this
    var count = 3 - this.data.tempFilePaths.length
    console.log("addImage:", count)
    wx.chooseImage({
      count: count,
      sizeType: ['compressed'],
      sourceType: ['album', 'camera'],
      success: function (res) {
        // tempFilePath可以作为img标签的src属性显示图片
        // this.data.tempFilePaths.concat(
        var pics = _this.data.tempFilePaths.concat(res.tempFilePaths)
        _this.setData({
          tempFilePaths: pics
        })
      }
    })
  },
  submit() {
    // wx.navigateBack({})
    wx.showLoading({
      title: '正在上传...',
      mask: true
    })
    if (this.data.tempFilePaths.length == 0) {
      var postData = {
        files: [],
        remark: this.data.content
      }
      // console.log("postData:",postData)
      httpApi.addFeedback(JSON.stringify(postData), this.data.title, this.data.communityId).then(res => {
        console.log("addFeedback:", res.data)
        wx.hideLoading()
        wx.navigateBack()
      })
    } else {
      console.log("上传的图片:", this.data.tempFilePaths)
      this.uploadimg({
        url: 'https://m.zhiliaoyuedu.cn/pic/upload', //这里是你图片上传的接口
        path: this.data.tempFilePaths //这里是选取的图片的地址数组
      });
    }
  },

  uploadimg(data) {
    var _this = this,
      i = data.i ? data.i : 0,
      success = data.success ? data.success : 0,
      fail = data.fail ? data.fail : 0;

    var token = wx.getStorageSync('token')
    wx.uploadFile({
      url: data.url,
      filePath: data.path[i],
      name: 'file',
      header: {
        'Authorization': token,
        'content-type': 'multipart/form-data'
      },
      formData: {

      },
      success: (res) => {
        var resData = JSON.parse(res.data)
        success++;
        _this.data.uploadUrls = _this.data.uploadUrls.concat(resData.data)
      },
      fail: (res) => {
        fail++; //图片上传失败，图片上传失败的变量+1
        console.log('fail:' + i + "fail:" + fail);
      },
      complete: () => {
        console.log(i);
        i++; //这个图片执行完上传后，开始上传下一张
        if (i == data.path.length) { //当图片传完时，停止调用          		
          var postData = {
            files: _this.data.uploadUrls,
            remark: _this.data.content
          }
          // console.log("postData:",postData)
          httpApi.addFeedback(JSON.stringify(postData), _this.data.title, _this.data.communityId).then(res => {
            console.log("addFeedback:", res.data)
            wx.hideLoading()
            wx.navigateBack()
          })
        } else { //若图片还没有传完，则继续调用函数
          console.log(i);
          data.i = i;
          data.success = success;
          data.fail = fail;
          _this.uploadimg(data);
        }

      }
    });
  },

  // 按钮点击
  btnClick() {
    // wx.navigateTo({ url: '../feedback' });
    //返回上一级页面
    wx.navigateBack({delta: 1})
  },
})
